package cn.gs.util.stream;

// @author gs
import java.io.*;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LineByLineFileReader extends BufferedReader {

    public LineByLineFileReader(File file) throws FileNotFoundException {
        super(new FileReader(file));
    }

    public LineByLineFileReader(String file) throws FileNotFoundException {
        super(new FileReader(file));
    }

    public LineByLineFileReader(Reader in) {
        super(in);
    }

    public LineByLineFileReader(Reader in, int sz) {
        super(in, sz);
    }

    @Override
    public void finalize() throws Throwable {
        super.finalize();
        super.clone();
    }

    public String readString() throws IOException {
        return super.readLine();
    }

    public Double readDouble() throws IOException {
        String s = readString();
        if (s == null) {
            return null;
        } else {
            return Double.valueOf(s);
        }
    }

    public Float readFloat() throws IOException {
        String s = readString();
        if (s == null) {
            return null;
        } else {
            return Float.valueOf(s);
        }
    }

    public Integer readInt() throws IOException {
        String s = readString();
        if (s == null) {
            return null;
        } else {
            return Integer.valueOf(s);
        }
    }

    public Boolean readBoolean() throws IOException {
        String s = readString();
        if (s == null) {
            return null;
        } else {
            return Boolean.valueOf(s);
        }
    }

    class FileStringIterator implements Iterator<String> {

        @Override
        public boolean hasNext() {
            try {
                return LineByLineFileReader.this.ready();
            } catch (IOException ex) {
                Logger.getLogger(LineByLineFileReader.class.getName()).log(Level.WARNING, null, ex);
                return false;
            }
        }

        @Override
        public String next() {
            try {
                return readString();
            } catch (IOException ex) {
                Logger.getLogger(LineByLineFileReader.class.getName()).log(Level.WARNING, null, ex);
                return null;
            }
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    class FileDoubleIterator implements Iterator<Double> {

        @Override
        public boolean hasNext() {
            try {
                return LineByLineFileReader.this.ready();
            } catch (IOException ex) {
                Logger.getLogger(LineByLineFileReader.class.getName()).log(Level.WARNING, null, ex);
                return false;
            }
        }

        @Override
        public Double next() {
            try {
                return readDouble();
            } catch (IOException ex) {
                Logger.getLogger(LineByLineFileReader.class.getName()).log(Level.WARNING, null, ex);
                return null;
            }
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    class FileIntIterator implements Iterator<Integer> {

        @Override
        public boolean hasNext() {
            try {
                return LineByLineFileReader.this.ready();
            } catch (IOException ex) {
                Logger.getLogger(LineByLineFileReader.class.getName()).log(Level.WARNING, null, ex);
                return false;
            }
        }

        @Override
        public Integer next() {
            try {
                return readInt();
            } catch (IOException ex) {
                Logger.getLogger(LineByLineFileReader.class.getName()).log(Level.WARNING, null, ex);
                return null;
            }
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    class FileBooleanIterator implements Iterator<Boolean> {

        @Override
        public boolean hasNext() {
            try {
                return LineByLineFileReader.this.ready();
            } catch (IOException ex) {
                Logger.getLogger(LineByLineFileReader.class.getName()).log(Level.WARNING, null, ex);
                return false;
            }
        }

        @Override
        public Boolean next() {
            try {
                return readBoolean();
            } catch (IOException ex) {
                Logger.getLogger(LineByLineFileReader.class.getName()).log(Level.WARNING, null, ex);
                return null;
            }
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
}
